//
//  ZSidebarViewController.m
//  SideBar
//
//  Created by 周成龙 on 2018/3/9.
//  Copyright © 2018年 XZL. All rights reserved.
//

#import "ZSidebarViewController.h"

#define kViewWidth [UIScreen mainScreen].bounds.size.width
#define kViewHeight [UIScreen mainScreen].bounds.size.height

@interface ZSidebarViewController ()

@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *mainView;
@property (nonatomic, weak) UIView *rightView;

@end

@implementation ZSidebarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加子视图
    [self setupSubViews];
    
    // 添加拖动手势
    [self addPanGesture];
}

- (void)setupSubViews {
    UIView *leftView = [UIView new];
    leftView.backgroundColor = [UIColor blueColor];
    leftView.frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
    [self.view addSubview:leftView];
    self.leftView = leftView;
    
    UIView *rightView = [UIView new];
    rightView.backgroundColor = [UIColor purpleColor];
    rightView.frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
    [self.view addSubview:rightView];
    self.rightView = rightView;
    
    UIView *mainView = [UIView new];
    mainView.backgroundColor = [UIColor orangeColor];
    mainView.frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
    [self.view addSubview:mainView];
    self.mainView = mainView;
}

- (void)addPanGesture {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.mainView addGestureRecognizer:pan];
}

#define targetR (kViewWidth - 100)
#define targetL -(kViewWidth - 100)

#define maxY 150

- (void)pan:(UIPanGestureRecognizer *)gesture {
    
    // 当前触摸点
    CGPoint transP = [gesture translationInView:self.mainView];
    // 如果不改变 view 的大小的话，可以使用这句代码 替换 计算 mainView 的位置
//    self.mainView.transform  = CGAffineTransformTranslate(self.mainView.transform, transP.x, 0);
    
    // 设置 mainView 的位置
    self.mainView.frame = [self mainViewFrameWithPoint:transP.x];

    // 显示隐藏 view
    CGFloat mainX = self.mainView.frame.origin.x;
    if (mainX > 0) {
        self.leftView.hidden = NO;
        self.rightView.hidden = YES;
    }else {
        self.leftView.hidden = YES;
        self.rightView.hidden = NO;
    }

    // 回弹
    if (gesture.state == UIGestureRecognizerStateEnded) {

        CGFloat target = 0;
        if (mainX > kViewWidth*0.5) {
            target = targetR;
        }else if (CGRectGetMaxX(self.mainView.frame) < kViewWidth*0.5) {
            target = targetL;
        }
        
        CGFloat offsetX = target - mainX;
        
        CGRect frame = [self mainViewFrameWithPoint:offsetX];
        
        [UIView animateWithDuration:0.25 animations:^{
            self.mainView.frame = frame;
        }];
    }
    
    [gesture setTranslation:CGPointZero inView:self.mainView];
}

- (CGRect)mainViewFrameWithPoint:(CGFloat)pointX {
    CGRect frame = self.mainView.frame;
    frame.origin.x += pointX;
    
    frame.origin.y = fabs(frame.origin.x * maxY/kViewWidth);
    frame.size.height = kViewHeight - 2*frame.origin.y;
    
    return frame;
}

@end
